<?php


namespace App\Models\Admin;


use App\Log\AdminDevLog;

class AdminDev extends Base
{
    const OPERATE_PID = 2;
    const MODEL = 'admin_dev';

    protected $table = 'admin_dev';
    protected $guarded = [];
    protected $dateFormat = 'U';

    public function sku_list()
    {
        return $this->hasMany(AdminDevSku::class,'dev_id','id');
    }

    public static function getList($data=[],$skip=0,$size=10)
    {
        $select=self::getWhere($data);
        $list=$select->with([
            'sku_list',
        ])
            ->orderBy('id','desc')
            ->skip($skip)->take($size)
            ->get();
        return $list;
    }

    public static function getWhere($data)
    {
        $select=self::where([]);
        if(!empty($data['name']))
        {
            $select=$select->where('name','like','%'.$data['name'].'%');
        }
        if(isset($data['status']))
        {
            $select=$select->where('status',$data['status']);
        }
        return $select;
    }

    public static function getCount($data)
    {
        $select=self::getWhere($data);
        $count=$select->count();
        return $count;
    }

    public static function getDetail($id)
    {
        $info=self::where('id',$id)->with([
            'sku_list'
        ])->first();
        return $info;
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
//            dd(12);
            $adminLog = new AdminDevLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
